extends Node


export var item_dict = {}


var resInventorySlotNode = load("res://Scenes/UI/InventorySlot.tscn")
var inventoryHUDNode = null
export var activeInventorySlot = ""

func _ready():
	reload()


func reload():
	item_dict.clear()
	var savedata = get_node("/root/Save").get_node_data_by_name("PlayerInventory")
	if savedata != null:
		var savedata_values = savedata.values()[0].values()
		#print (savedata_values[0])
		item_dict = savedata_values[1]

		if savedata_values[0] != "":
			activeInventorySlot = savedata_values[0]
		else:
			activeInventorySlot = ""

		
	reloadInventoryHUD()	


func gem_collected(type):
	if item_dict.has(str(type)):
		item_dict[str(type)] += 1
		var inventorySlotNode = inventoryHUDNode.find_node("Slot" + str(type), true, false)
		if inventorySlotNode != null:
			updateInventoryCounter(type, inventorySlotNode)
			
	else:
		item_dict[str(type)] = 1
		addItemSlot(type)
	print ("gem collected: " + str(type))
	
	#debug use
	#var iSN = inventoryHUDNode.find_node("Slot" + str(type), true, false)
	#setActiveInventorySlot(iSN)
	
func save():
	var save_dict = {
		item_dict = item_dict,
		activeInventorySlot = activeInventorySlot
	}
	return save_dict
	

func reloadInventoryHUD():
	var currentWorld = get_node("/root/global").getCurrentWorld()
	inventoryHUDNode = currentWorld.find_node("HUDInventory", true, false)
	if inventoryHUDNode != null:
		print ("inventoryHUDNode found")
		
		#first delete all slots
		for node in inventoryHUDNode.get_children():
			node.name = node.name + "_" # rename nodes before deleting, 
			node.queue_free()
		
		for item_id in item_dict:
			addItemSlot(item_id)
			


		#else:
		#	print ("set active slot to null")
		#	setActiveInventorySlot(null)

func addItemSlot(item_id):
	if inventoryHUDNode != null:
		
		var newInventorySlotNode = resInventorySlotNode.instance()
		newInventorySlotNode.set_name("Slot" + str(item_id))
		if activeInventorySlot == newInventorySlotNode.name:
			setActiveInventorySlot(newInventorySlotNode)
		
		setInventoryImage(item_id, newInventorySlotNode)
		updateInventoryCounter(item_id, newInventorySlotNode)
		
		#add sortName to the node here
		var sortName = get_node("/root/GameItemList").get_item_value(item_id, "sortName")
		if sortName == null:
			print ("Warning: item has no sortName")
			return
		
		newInventorySlotNode.sort_name = str(sortName)
		
		if inventoryHUDNode.get_child_count() > 0:		
			inventoryHUDNode.add_child(newInventorySlotNode)
			
			
			#### sort: todo: extract code into own function
			
			var current_pos = null
			var current_sort_index = newInventorySlotNode.get_index()
			
			for ch in inventoryHUDNode.get_children():
				if sortName.casecmp_to (ch.sort_name ) == -1 and current_sort_index > ch.get_index():
					current_pos = ch
					current_sort_index = ch.get_index()
				
					
			if current_pos != null:
				inventoryHUDNode.move_child ( newInventorySlotNode, current_pos.get_index() )
			
			####
			
			
		else:
			inventoryHUDNode.add_child(newInventorySlotNode)
			if activeInventorySlot == "":
				setActiveInventorySlot(newInventorySlotNode)


func setInventoryImage(item_id, inv_slot_node):
	var inventoryImageName = get_node("/root/GameItemList").get_item_value(item_id, "inventoryImage")
	if inventoryImageName == null:
		return
		
	var inventoryImagePath = "res://Textures/UI/" + str(inventoryImageName)
	var inventoryImage  = load(inventoryImagePath)
	if inventoryImage == null:
		return
		
	var inventoryImageNode = inv_slot_node.find_node("InventoryImage", true, false)
	if inventoryImageNode != null:
		inventoryImageNode.texture = inventoryImage

func updateInventoryCounter(item_id, inv_slot_node):
	var counterLabelNode = inv_slot_node.find_node("CounterLabel", true, false)
	if item_dict.has(str(item_id)) and counterLabelNode != null:
		counterLabelNode.text = str(item_dict[str(item_id)])

	
func setActiveInventorySlot(inventorySlotNode):
	if inventorySlotNode == null:
		activeInventorySlot = ""
		print ("error404: inventorySlotNode is null")
		return

	print (activeInventorySlot)

	if activeInventorySlot != "":
		
		var oldInventorySlotNode = inventoryHUDNode.find_node(activeInventorySlot, true, false)
		if oldInventorySlotNode != null:
			oldInventorySlotNode.set_as_active_slot(false)
		
	activeInventorySlot = inventorySlotNode.name
	
	if activeInventorySlot != "":

		inventorySlotNode.set_as_active_slot(true)


func _input(event):
	
	var inventorySlotNode = inventoryHUDNode.find_node(activeInventorySlot, true, false)
	var inventorySlotNodePos = -1
	if inventorySlotNode != null:
		inventorySlotNodePos = inventorySlotNode.get_position_in_parent()
	
		if event is InputEventMouseButton and event.pressed:
			match event.button_index:
				BUTTON_WHEEL_UP:
				
					var rightNeighborNode = inventoryHUDNode.get_child(inventorySlotNodePos + 1)
					if rightNeighborNode != null:
						print ("can change >>>")
						setActiveInventorySlot(rightNeighborNode)
					
					print ("mouse up: " + str(inventorySlotNodePos))
				
				BUTTON_WHEEL_DOWN:
				
					var leftNeighborNode = inventoryHUDNode.get_child(inventorySlotNodePos - 1)
					if leftNeighborNode != null:
						print ("can change <<<")
						setActiveInventorySlot(leftNeighborNode)
				
					print ("mouse down: " + str(inventorySlotNodePos))
			



#save active slot (position ?, id ?) and load

#add action
